package cn.com.surker.workcon.service.zhonglian;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.control.entity.ControlInvoice;
import cn.com.surker.production.dao.IndentDispatchDao;
import cn.com.surker.production.entity.ProductionIndentDispatch;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.ProductService;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 中青林工控机的生产数据 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public class ProductServiceImpl extends GKJDBCService implements ProductService {

	@Resource
	private IndentDispatchDao indentDispatchDao;

	/**
	 * 根据erp的派单信息,查询派车数据
	 *
	 * @param dispatchId erp的派单id
	 * @return
	 */
	public ProductionIndentDispatch queryByDispatchId(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		ProductionIndentDispatch dispatch = indentDispatchDao.selectById(dispatchId);
		//	派单编码
		String ProductRegNo = dispatch.getCode();
		//	派单序号
		Long PRegOrder = dispatch.getId();
		//	查询数据
		String queryDisSql = "select status from ProductRegist where ProductRegNo = ? and PRegOrder = ?";
		//	参数
		Object[] disParam = {ProductRegNo, PRegOrder};
		List<Map<String, Object>> disResL = lineJdbcTemp.queryForList(queryDisSql, disParam);
		if (disResL != null && disResL.size() > 0 && disResL.get(0) != null) {
			//	存在数据,看是否小于0;派单表中的status=0表示等待生产;>0表示正在生产;小于0或者删除表示生产完成
			Map<String, Object> map = disResL.get(0);
			Object statusObj = map.get("status");
			Integer status = (Integer) statusObj;
			if (status >= 0) {
				//	没有生产完成
				return null;
			}
		}
		//	生产完成了
		//	查询工控数据 TODO 是否加入任务单号查询
		String queryProSql = "select a.id,a.CurCarNum,a.EndTime,a.ConFormulaNo,a.Status \n" +
			"from History a\n" +
			"left join ProductRegist b ON a.ProductRegID = b.ID\n" +
			"where b.ProductRegNo = ? and b.PRegOrder = ?";
		Object[] proParam = {ProductRegNo, PRegOrder};
		//	查询数据
		List<Map<String, Object>> proResList = lineJdbcTemp.queryForList(queryProSql, proParam);
		if (proResList == null || proResList.isEmpty()) {
			//	没有查询到数据
			return null;
		}
		//	取出数据
		Map<String, Object> proRes = proResList.get(0);
		Date prodTim = null;
		String prodTimE = String.valueOf(proRes.get("EndTime"));
		if (StringUtils.isNotBlank(prodTimE)) {
			prodTim = DateUtil.convertDate(prodTimE);
		}
		Double prodMete = Double.valueOf(String.valueOf(proRes.get("CurCarNum")));
		String prodId = String.valueOf(proRes.get("id"));
		String proStr = String.valueOf(proRes.get("Status"));
		Integer controlProStatus = Integer.valueOf(proStr);
		//	查询配合比编号
		String recipe = String.valueOf(proRes.get("ConFormulaNo"));
		//	查询派车数据
		ProductionIndentDispatch pid = new ProductionIndentDispatch();
		pid.setProTime(prodTim);
		pid.setProStere(prodMete);
		pid.setControlId(prodId);
		pid.setControlProStatus(controlProStatus);
		pid.setRecipe(recipe);
		return pid;
	}


	@Override
	public Boolean inProing(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		//	查询派单表的状态是否大于0
		ProductionIndentDispatch dispatch = indentDispatchDao.selectById(dispatchId);
		//	派单编码
		String ProductRegNo = dispatch.getCode();
		//	派单序号
		Long PRegOrder = dispatch.getId();
		//	查询数据
		String querySql = "select status from ProductRegist where ProductRegNo = ? and PRegOrder = ?";
		//	参数
		Object[] param = {ProductRegNo, PRegOrder};
		List<Map<String, Object>> maps = lineJdbcTemp.queryForList(querySql, param);
		if (maps == null || maps.isEmpty()) {
			//	查询是否已经生产完成
			Boolean aBoolean = hasProduct(lineJdbcTemp, PRegOrder, ProductRegNo);
			if (aBoolean) {
				//	已经生产完成,返回true
				return true;
			}
			return null;
		}
		Map<String, Object> map = maps.get(0);
		Object statusObj = map.get("status");
		Integer status = (Integer) statusObj;
		if (status > 0) {
			//	正在生产
			return true;
		}
		if (status == 0) {
			//	等待生产
			return false;
		}
		//	没有数据或者小于0 ,判断是否生产了
		Boolean aBoolean = hasProduct(lineJdbcTemp, PRegOrder, ProductRegNo);
		if (aBoolean) {
			//	已经生产完成,返回true
			return true;
		}
		return false;
	}

	@Override
	public List<ControlInvoice> findFullByMin(Long lineId, String lineName, Date minDate, Long minProdId) {
		if (lineId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		//	查询数据
		String querySql = "select a.ProjectName conName,a.CustomerName clientName,a.ProjectAddr address,a.LinkMan linkMan,a.Tel linkPhone,a.ConstructPlace site,\n" +
			"\ta.ConcreteStrength strengthName,a.Sink slumps,a.PouringMode castModel,a.ConcreteSumNum concreteStere,a.MortarNum mortarStere,a.CarID carCode,\n" +
			"\ta.SumCarNum totalCar,a.PTaskListNo carNo,a.SysOperator mixer,a.EndTime proTime,a.id prodId,a.ProductLineID lineId,a.ProductLineName lineName,\n" +
			"\ta.id controlInvoiceNo,a.Remark controlRemark,a.PTaskListID indentCode,b.PRegOrder erpId\n" +
			"from History a\n" +
			"left join ProductRegist b On a.ProductRegID = b.id\n";
		if (minProdId != null) {
			querySql += "and a.id > " + minProdId;
		} else if (minDate != null) {
			querySql += "and EndTime > '" + DateUtil.convertString(minDate, "yyyy-MM-dd HH:mm:ss") + "'";
		}
		querySql += " order by prodTimE;";
		Object[] params = {lineId, lineName};
		List<ControlInvoice> weightSpotChecks = lineJdbcTemp.query(querySql, params, BeanPropertyRowMapper.newInstance(ControlInvoice.class));
		return weightSpotChecks;
	}

	@Override
	public List<ControlInvoice> findByTime(Long lineId, String lineName, String startTime, String endTime) {
		return null;
	}


	private Boolean hasProduct(JdbcTemplate lineJdbcTemp, Long dispatchId, String disCode) {
		//	查询sql
		String querySql = "select a.id \n" +
			"from History a\n" +
			"left join ProductRegist b ON a.ProductRegID = b.ID\n" +
			"where b.ProductRegNo = ? and b.PRegOrder = ?";
		Object[] param = {disCode, dispatchId};
		//	查询数据
		List<Map<String, Object>> maps = lineJdbcTemp.queryForList(querySql, param);
		if (maps != null && maps.size() > 0 && maps.get(0) != null) {
			//	有生产数据
			return true;
		}
		return false;
	}
}
